import com.codezen.helper.Worker;
import com.codezen.util.CUtils;

import flash.events.Event;

import mx.collections.ArrayCollection;
import mx.collections.Sort;
import mx.collections.SortField;
import mx.core.FlexGlobals;
import mx.utils.ObjectUtil;

private var sort:Sort;

private var hdDef:Array = [
	["1", "360"],
	["2", "480"],
	["3", "720"]
];

private var queryParts:Array;

private var selectedEpisodeName:String;

private function filterSeries():void{
	sort = new Sort();
	sort.fields = [new SortField('number', false, true, true)];
	
	seriesList.filterFunction = function(obj:Object):Boolean{
		return obj.season == int(seasons_list.selectedItem);
	};
	seriesList.sort = sort;
	seriesList.refresh();
}

private function onEpisodeSelect(event:Event):void{
	series_source_wnd.visible = false;
	series_source_wnd.x = mouseX;
	if( (series_source_wnd.x+series_source_wnd.width) > width){
		series_source_wnd.x = width - series_source_wnd.width;
	}
	series_source_wnd.y = mouseY - series_source_wnd.height;
	if( series_source_wnd.y < 52){
		series_source_wnd.y = 55;
	}
	
	// busy
	setBusyState();
	
	// set sel ep
	if(String(series_list.selectedItem.name).length <= 0){
		selectedEpisodeName = kb.seriesInformation.name + "\n" + series_list.selectedItem.original_name;
	}else{
		selectedEpisodeName = kb.seriesInformation.name + "\n" + series_list.selectedItem.name;
	}
	
	// form query
	queryParts = new Array();
	queryParts['name'] = kb.seriesInformation.name;
	queryParts['ename'] = kb.seriesInformation.original_name;
	queryParts['season'] = series_list.selectedItem.season;
	queryParts['number'] = series_list.selectedItem.number;
	queryParts['len'] = 20;
	var query:String = kb.seriesInformation.name + " " + series_list.selectedItem.season +
		" " + series_list.selectedItem.number;
	
	findQuery(query, 10);
	
	// deselect all
	series_list.selectedIndex = -1;
}



private function sourceLabel(item:Object):String{
	var name:String = "[";
	if(int(item.hd) == 0){
		name += "260";
	}else{
		name += hdDef[int(item.hd)-1][1];
	}
	name += "p] "+String(item.title).slice(0,50)+"..";
	name += " ("+item.len+")";
	return name;
}

private function onSourceSelect(e:Event):void{
	// if nothing selected - exit
	if(series_source_list.selectedIndex < 0) return;
	// save url
	var url:String = series_source_list.selectedItem.url;
	// hide window
	if(seasons_list.visible)
		series_source_wnd.visible = false;
	
	// change state to player
	currentState = "VideoPlayer";
	
	// set label
	video_player.video_title.text = selectedEpisodeName;
	
	// play
	video_player.loadVideoAndPlay(url);
}